package com.example.selfservicestation.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.viewbinding.ViewBinding
import com.example.selfservicestation.R
import com.example.selfservicestation.activity.MainActivity
import com.example.selfservicestation.global.GlobalParams

abstract class BaseBindingFragment<T : ViewBinding>(private val bindingInflater: (layoutInflater: LayoutInflater) -> T) : Fragment() {

    // Bindings
    private var _binding: T? = null

    /**
     * Binding
     */
    protected val binding get() = _binding!!

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        _binding = bindingInflater.invoke(inflater)
        return binding.root
    }


    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    fun showLoading(){
        (requireActivity() as MainActivity).showLoading()
    }

    fun dismissLoading(){
        (requireActivity() as MainActivity).dismissLoading()
    }

    override fun onResume() {
        super.onResume()
        (activity as MainActivity).hideNavigationBar(requireActivity())
        GlobalParams.notifyResultFlow.tryEmit(findNavController().currentDestination?.id != R.id.homeFragment)
    }
}